const app = getApp()
import BaseModel from '../../utils/common.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:"",//客户id
    selectTypeId:0,//选中的类型id
    selectTypeText:"",//选中的类型text
    selectLevelId:0,//选中的级别id
    selectLevelText:"",//选中的级别text
    selectStateId:0,//选中的状态id
    selectStateText:"",//选中的状态text
    screenTypeData:[{
      id:1,
      selected:false,
      name:"客户"
    },{
      id:2,
      selected:false,
      name:"渠道商"
    },{
      id:3,
      selected:false,
      name:"合作伙伴"
    },{
        id:4,
        selected:false,
        name:"其他"
      }], //类型数据
    screenLevelData:[{
      id:1,
      selected:false,
      name:"一般"
    },{
      id:2,
      selected:false,
      name:"重要"
    },{
      id:3,
      selected:false,
      name:"核心"
    }], //级别数据
    screenStateData:[{
      id:1,
      selected:false,
      name:"潜在"
    },{
      id:2,
      selected:false,
      name:"意向"
    },{
      id:3,
      selected:false,
      name:"洽谈"
    },{
      id:4,
      selected:false,
      name:"成交"
    },{
      id:5,
      selected:false,
      name:"流失"
    }], //状态数据
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id:options.id,
      selectTypeId:options.selectTypeId,
      selectTypeText:options.selectTypeText,
      selectLevelId:options.selectLevelId,
      selectLevelText:options.selectLevelText,
      selectStateId:options.selectStateId,
      selectStateText:options.selectStateText,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getLabelData(); //获取已选标签
  },
  //类型点击事件
  selectType:function(e){
    this.setData({
      selectTypeId:e.currentTarget.dataset.id,
      selectTypeText:e.currentTarget.dataset.text,
    })
  },
  //级别点击事件
  selectLevel:function(e){
    this.setData({
      selectLevelId:e.currentTarget.dataset.id,
      selectLevelText:e.currentTarget.dataset.text,
    })
  },
  //状态点击事件
  selectState:function(e){
    this.setData({
      selectStateId:e.currentTarget.dataset.id,
      selectStateText:e.currentTarget.dataset.text,
    })
  },
  /*确定*/
  click_sure:function(){
    var that = this;
    if(that.data.id==undefined||that.data.id==""){
      var pages = getCurrentPages();
      var prevPage = pages[pages.length - 2];  //上一个页面
      prevPage.setData({
        selectTypeId: that.data.selectTypeId,
        selectTypeText: that.data.selectTypeText,
        selectLevelId: that.data.selectLevelId,
        selectLevelText: that.data.selectLevelText,
        selectStateId: that.data.selectStateId,
        selectStateText: that.data.selectStateText,
      })        //给上级页面的变量赋值
      wx.navigateBack()  //返回上级页面
    }else{
      this.getUserLabelData();
    }
  },
  //修改标签
  getUserLabelModel:new BaseModel({
    url: "updateLabel",
    needAjaxLoading:true,
    method: "get",
    loadingText:"玩命加载中"
  }),
  getUserLabelData(){
    this.getUserLabelModel
        .fetch({
          selectTypeId:this.data.selectTypeId,
          selectLevelId:this.data.selectLevelId,
          selectStateId:this.data.selectStateId,
          id:this.data.id,
        })
        .then(res => {
          //成功
          var pages = getCurrentPages();
          var prevPage = pages[pages.length - 2];  //上一个页面
          prevPage.setData({
            id: this.data.id,
          })        //给上级页面的变量赋值
          wx.navigateBack()  //返回上级页面
        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
//获取已选标签
  getLabelModel: new BaseModel({
    url: "getLabel",
    needAjaxLoading: true,
    method: "get",
    loadingText: "玩命加载中"
  }),
  getLabelData() {
    this.getLabelModel
        .fetch({
          id: this.data.id,
        })
        .then(res => {
          //成功
          this.setData({
            selectTypeId:res.data.type,
            selectLevelId:res.data.level,
            selectStateId:res.data.fare
          });

        })
        .catch(res => {
          //失败 2018-12-20 00:00:00
        })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getLabelData(); //获取已选标签
    wx.stopPullDownRefresh(); //停止下拉
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})